﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Mabna.Total.BOHelper;
using EShop.Common;
using EShop.BussinessObjects;
using EShop.DataAccess;
using EShop.BusinessLogic;

public partial class UserControls_Signup : System.Web.UI.UserControl,ICallbackEventHandler
{

    public string Mode
    {
        get
        {
            return hdnMode.Value;
        }
        set
        {
            hdnMode.Value = value;
        }
    }

    public string ClientEventafterInsert = "Clientevent_afterInsert";

    string strResult = ""; 
    protected void Page_Load(object sender, EventArgs e)
    {
        #region CallBack Scripts

        string cbReference =Page.ClientScript.GetCallbackEventReference(this,"arg", "signupReceive", "context", true);

        string callbackScript;
        callbackScript = "function signupCallServer(arg, context)" +
            "{" + cbReference + "} ;";
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
            "signupCallServer", callbackScript, true);

        #endregion

        if (!IsPostBack)
        {
            this.FillCoverdAreas();
        }
    }

    private void FillCoverdAreas()
    {
        CompanyDeliveryDistrict companyDeliveryDistrict = new CompanyDeliveryDistrict();
        companyDeliveryDistrict.ID = (int)cnstDatabase.DBOperationResult.ValueIsNotEffective;
        companyDeliveryDistrict.Company = new Company();
        companyDeliveryDistrict.Company.ID = CGlobalData.GetCurrentCompanyID();
        companyDeliveryDistrict.PostalCode = ((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString();
        List<CompanyDeliveryDistrict> companyDeliveryDistrictList = CDACompanyDeliveryDistrict.Select(companyDeliveryDistrict);

        foreach (CompanyDeliveryDistrict _companyDeliveryDistrict in companyDeliveryDistrictList)
        {
            ListItem item=new ListItem();
            item.Text = _companyDeliveryDistrict.PostalCode;
            item.Value = _companyDeliveryDistrict.ID.ToString();
            drpPostalCode.Items.Add(item);
        }
    }

    public void FillUserControl(Contact contact)
    {
        if (Mode == "EditProfile")
        {
            hdnContact.Value = contact.ToJSON();
        }
    }

    #region ICallbackEventHandler Members

    public string GetCallbackResult()
    {
        return strResult;
        
    }

    public void RaiseCallbackEvent(string eventArgument)
    {
        List<Object> ClientData=(List<Object>)ObjectSerializer.FromJson(eventArgument,typeof(List<Object>));

        string Command = ClientData[0].ToString();
        List<Object> ServerData = new List<object>();
        #region Contact
        if (Command == "Contact")
        {
            Contact _contact=(Contact)ObjectSerializer.FromJson(ClientData[1].ToString(),typeof(Contact));
            
            string Msg="";
            bool State=CDAContact.Insert(_contact, out Msg);
            if (State)
            {
                ServerData.Add("Contact");
                ServerData.Add("Success");
                ServerData.Add(_contact.ID);
                strResult = ServerData.ToJSON();
            }
            else
            {
                ServerData.Add("Contact");
                ServerData.Add("Fail");
                ServerData.Add(Msg);
                strResult = ServerData.ToJSON();
            }
        }
        #endregion
        #region SignUp
        if (Command == "SignUp")
        {
            Contact _contact = (Contact)ObjectSerializer.FromJson(ClientData[1].ToString(), typeof(Contact));
            User _user = (User)ObjectSerializer.FromJson(ClientData[2].ToString(), typeof(User));

            string Msg = "";
            bool ContactState = CDAContact.Insert(_contact, out Msg);
            if (ContactState)
            {
                _user.UserType = new UserType();
                _user.UserType.ID = cnstUserType.Customer;
                _user.Contact = new Contact();
                _user.Contact.ID = _contact.ID;
                bool UserState = CBLUser.Insert(_user, out Msg);
                if (UserState)
                {
                    
                    ServerData.Add("SignUp");
                    ServerData.Add("Success");
                    ServerData.Add(_contact.ID);
                    ServerData.Add(_user.ID);
                    strResult = ServerData.ToJSON();
                }
                else
                {
                    string delerror = "";
                    CDAContact.Delete(_contact, out delerror);
                    ServerData.Add("SignUp");
                    ServerData.Add("Fail");
                    ServerData.Add(Msg);
                    strResult = ServerData.ToJSON();
                }
                
            }
            else
            {
                ServerData.Add("SignUp");
                ServerData.Add("Fail");
                ServerData.Add(Msg);
                strResult = ServerData.ToJSON();
            }

        }
        #endregion
        #region EditProfile
        if (Command == "EditProfile")
        {
            Contact _contact = (Contact)ObjectSerializer.FromJson(ClientData[1].ToString(), typeof(Contact));
            User _user = (User)ObjectSerializer.FromJson(ClientData[2].ToString(), typeof(User));

            string Msg = "";
            _contact.ID = CGlobalData.GetCurrentContactID();
            bool ContactState = CDAContact.Update(_contact, out Msg);
            if (ContactState)
            {
                _user.ID = CGlobalData.GetCurrentUserID();
                _user.Contact = new Contact();
                _user.Contact.ID = _contact.ID;
                _user.UserType = new UserType();
                _user.UserType.ID = cnstUserType.Customer;
                if (_user.Password == "")
                {
                    User user = CGlobalData.GetCurrentUser();
                    _user.Password = user.Password;
                }
                bool UserState = CBLUser.Update(_user, out Msg);
                if (UserState)
                {
                    ServerData.Add("EditProfile");
                    ServerData.Add("Success");
                    strResult = ServerData.ToJSON();
                }
                else
                {
                    ServerData.Add("EditProfile");
                    ServerData.Add("Fail");
                    ServerData.Add(Msg);
                    strResult = ServerData.ToJSON();
                }

            }
            else
            {
                ServerData.Add("EditProfile");
                ServerData.Add("Fail");
                ServerData.Add(Msg);
                strResult = ServerData.ToJSON();
            }

        }
        #endregion
    }

    #endregion
}
